'''
https://leetcode.cn/problems/best-time-to-buy-and-sell-stock-iii/description/
'''

from typing import List


class Solution:

    def maxProfit(self, prices: List[int]) -> int:
        # 买了，或买了，后的手中余额
        buy1, sell1 = 0 - prices[0], 0
        buy2, sell2 = 0 - prices[0], 0
        for price in prices:
            buy1 = max(buy1, 0 - price)
            sell1 = max(sell1, buy1 + price)

            buy2 = max(buy2, sell1 - price)
            sell2 = max(sell2, buy2 + price )
        return max(sell1, sell2)
